import Web3 from 'web3'
import USDT from './bnbUsdt.json'
import DAPP from './dapp.json'
import BIND from './bind.json'
import FPC from './fpc.json'
import GK from './gk.json'
import LP from './lp.json'
import GG from './gg.json'
import JS from './js.json'
import SWARP from './swarp.json'
import {adlist} from './addlist'
import store from '../store'
import { Toast } from 'vant';
// const { ethers } = require("ethers");
//正式
// const ADDRESS_DAPP ="0xd97234BCa79c0EB5515cC4e5Af93f99fC2082099"//主合约
// const ADDRESS_FPC ="0x270db1358993Ab42cf09c681308689Cb65A62619"//单币合约
// const ADDRESS_GK ="0xd49fC3E030F9dEA2437BA1069bF9A59026C00eDD"//国债合约
// const ADDRESS_LP ="0x5434987AeFD45Fd719C77c1Cb7655F57a827D3c3"//Lp合约
// const ADDRESS_JJC ="0xCeA188c6bb0dB78Fd17904C03e28F43AE4e46d5a"//奖金池合约
// const ADDRESS_ARB ="0xB192D4A737430AA61CEA4Ce9bFb6432f7D42592F" //代币合约
// const ADDRESS_USDT ="0x55d398326f99059fF775485246999027B3197955" //USDT合约
// const ADDRESS_BIND="0x852E1988d55085f66BE7c7f07ccD13aDFD0D1059" //绑定关系合约
// const ADDRESS_WARP="0xa1e08E10Eb09857A8C6F2Ef6CCA297c1a081eD6B"//交易对合约
// const ADDRESS_BACK="0x000000000000000000000000000000000000dEaD"//黑洞地址
// const ADDRESS_GG="0xe6d883D4147C23348760d1D135A2E187825979e1" //公共合约
// const ADDRESS_JS="0x2fBb00162dE57eb24408E653B766c5Fc5529cDa0"//结算合约
// const ADDRESS_GKHY='0x0fa6c0Bd9704a7a45B16e0487549B10218A82bAa' //国库合约

// 测试
const ADDRESS_DAPP ="0xD62B72605b7638bf17A664A0C7a6375F333F7076"//主合约
const ADDRESS_FPC ="0x9Ced8F645B14248a16aeD2CF9f4849BF97577FA4"//单币合约
const ADDRESS_GK ="0xE43B1fAA1eE45cf3a42EF4bbEF463bb25e89056E"//国债合约
const ADDRESS_LP ="0xfd347DcFD31aCC06d6930bA87B7999a3d0775687"//Lp合约
const ADDRESS_JJC ="0x21ADa66E18B74f62104c1f5c87608AF7243ac2A3"//奖金池合约
const ADDRESS_ARB ="0xCaCD5B2C714Ad262Eb0b705E66B5A99cc6EE12A2" //代币合约
const ADDRESS_USDT ="0x55d398326f99059fF775485246999027B3197955" //USDT合约
const ADDRESS_BIND="0x852E1988d55085f66BE7c7f07ccD13aDFD0D1059" //绑定关系合约
const ADDRESS_WARP="0x2344EBAba28c2ee08DD2740D71F5da00A1a44eeB"//交易对合约
const ADDRESS_BACK="0x000000000000000000000000000000000000dEaD"//黑洞地址
const ADDRESS_GG="0x52aDb647F8f05A28Be72FA7B342674075A6a7D88" //公共合约
const ADDRESS_JS="0x387115B387a4454A0bA0F05531EA088136E5663A"//结算合约
const ADDRESS_GKHY='0x23f07Fe5b5F8e71E0304b066235956aD8da909Cc' //国库合约

let ethereum = window.ethereum
// const provider = new ethers.providers.Web3Provider(window.ethereum);
// const signer = provider.getSigner();
    let i = setInterval(() => {
    if ((typeof window.ethereum) !== 'undefined') {
        ethereum = window.ethereum    
          
        handleWatch()  
        clearInterval(i)  
    }
    }, 1000);

export async function set(){
    ethereum = window.ethereum
}


export async function handleWatch () {
    if (!window.ethereum) {
         // //console.log('Metamask is not installed!')
        return false
      }
    window.ethereum.on('accountsChanged',  (accounts) => {
      if(accounts[0]){
        // let web3s = new Web3(ethereum);
        // let msg = web3s.utils.keccak256('授权签名');
        // let ht= await web3s.eth.sign(msg, accounts[0])
        // .then(res=>{return res});  
        // if(ht){ 
      sessionStorage.setItem('userWallet',accounts[0])  
      store.commit("userWallet", accounts[0]);  
    }else{
        sessionStorage.removeItem('userWallet')
      }     
    })
  }
export async function connectTron() {
    let web3s = new Web3(ethereum);
    //  // //console.log(web3s.utils.toDecimal(ethereum.chainId),ethereum.chainId);
    if (web3s.utils.toDecimal(ethereum.chainId)!='0x38') {

        try {
            await window.ethereum.request({
                method: 'wallet_switchEthereumChain',
                params: [{
                    chainId: Web3.utils.numberToHex(56), // 目标链ID
                }]
            })
        } catch (e) {
            if (e.code == 4902) {
                try {
                    await window.ethereum.request({
                        method: 'wallet_addEthereumChain',
                        params: [{
                            chainId: Web3.utils.numberToHex(56), // 目标链ID
                            chainName: 'Arbitrum One',
                            nativeCurrency: {
                                name: 'BNB',
                                symbol: 'BNB',
                                decimals: 18
                            },
                            rpcUrls: ['https://arbitrum-mainnet.infura.io'], // 节点
                            blockExplorerUrls: ['https://explorer.arbitrum.io']
                        }]
                    })
                    return true
                } catch (ee) {
                     // //console.log('//---------', ee);

                }
            } else if (e.code === 4001) return false
        }    
    }
    let isWallet =""
 // //console.log('window----',window);
    if (window.ethereum) {
      try {
        await  window.ethereum.request({
          method: 'wallet_requestPermissions',
          params: [{ eth_accounts: {} }]
        }).then(accounts => {
          isWallet=accounts
          // 在这里可以调用其他方法，如 ethereum.request({ method: 'eth_sendTransaction', ... }) 来发送交易
        }).catch(error => {
          console.error('用户拒绝授权或发生错误:', error);
        });
      } catch (error) {
        isWallet=true
      }
      // 检查浏览器是否支持以太坊钱包扩展
    
    } else {
       // //console.log('请使用支持以太坊钱包的浏览器（如MetaMask）');
    }

  // store.commit("userWallet", eth_requestAccounts[0]);
    if(isWallet){
      const eth_requestAccounts= await ethereum.request({ method: "eth_requestAccounts" });
      let result= adlist.find(el=>{ return el.toUpperCase()==eth_requestAccounts[0].toUpperCase()})
    return result?[]:eth_requestAccounts
    }
    // return eth_requestAccounts
}
function Exchan(){
  ethereum.enable()
            .catch(function(reason) {
                //如果用户拒绝了登录请求
                if (reason === "User rejected provider access") {
                    // 用户拒绝登录后执行语句；
                } else {
                    // 本不该执行到这里，但是真到这里了，说明发生了意外
                    Message.warning("There was a problem signing you in");
                }
            }).then(function(accounts) {
                // 判断是否连接以太
                let web3 = new Web3(ethereum);
                let currentProvider = web3.currentProvider;
                web3.setProvider(currentProvider);
                
                //如果用户同意了登录请求，你就可以拿到用户的账号
                web3.eth.defaultAccount = accounts[0];
                 // //console.log(accounts[0]);
                // myContract = new web3.eth.Contract(ABI, metaMaskAddress);
                //这里返回用户钱包地址
                callback(accounts[0]);
            });
}
// 签名认证
export function Sign(address) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let msg = web3s.utils.utf8ToHex('Welcome to Future protocol');
         // //console.log(msg,address);
        let ht= await web3s.eth.personal.sign(msg, address)
        .then(res=>{return res}).catch(err=>{
          // Toast(err)
           // //console.log(err);
          return false});
       return ht?ht:false
  };
  return run();
  }
// 查询是否要绑定上级
  export function getCanBind(address) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(BIND, ADDRESS_BIND);
          let result = await unioContract.methods.inviter(address).call();
        
         return result==0?true:false
    };
    return run();
  }
// 查询上级
  export function getinviter(address) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(BIND, ADDRESS_BIND);
          let result = await unioContract.methods.inviter(address).call();
           // //console.log(result);
         return result
    };
    return run();
  }
  
// 绑定邀请人
export  function toBind(address,referrer){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(BIND, ADDRESS_BIND);
       let gas =await web3s.eth.getGasPrice()
        let result = await unioContract.methods.bindInviter(referrer).send({from:address, gasPrice:gas}).catch(err => {
             // //console.log(err.message)
            return false
        });
         // //console.log(result);
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();

}
// 我的社区显示直推信息
export async function getReferralsInfo(address) {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(BIND, ADDRESS_BIND);  
    let result = await unioContract.methods.getInvites(address).call(); 
    //  // //console.log(result);
    if(result){
        return  result
    }else{
        return ""
    }
  };
  return run();
}
// 团队人数
export async function getMyteamCount(address) {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(BIND, ADDRESS_BIND);  
    let result = await unioContract.methods.teamCount(address).call();     
     return  result    
  };
  return run();
}

// 主合约
 // 查询代币是否授权
 export function getProved(address) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(USDT, ADDRESS_USDT, {
              from:address
          });
          let result = await unioContract.methods.allowance(address,ADDRESS_DAPP).call()/(10**18);
         return result 
    };
    return run();
  }
// 授权USDT
export function toApprove(address,num) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(USDT, ADDRESS_USDT);  
        let amount = web3s.utils.toWei(num + '','ether');
        let gas = await web3s.eth.getGasPrice();
        let result = await unioContract.methods.approve(ADDRESS_DAPP, amount).send({from:address, gasPrice:gas}).catch(err => {
             // //console.log(err.message)
         return false
      });
      if(result){
        return result.transactionHash
    }else{
        return false
    }
    };
    return run();
  }
//   用户提现生成涡轮池订单
export function toSetWithdraw(address,num) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
        let amount = web3s.utils.toWei(num + '','ether');
        let gas = await web3s.eth.getGasPrice();
        let result = await unioContract.methods.withdraw(amount).send({from:address, gasPrice:gas}).catch(err => {
             // //console.log(err.message)
         return false
      });
      if(result){
        return result.transactionHash
    }else{
        return false
    }
    };
    return run();
  }


// 涡轮订单领取
export function toGetClaimTurboOrder(address,orderId) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
        let gas = await web3s.eth.getGasPrice();
        let result = await unioContract.methods.claimTurboOrder(orderId).send({from:address, gasPrice:gas}).catch(err => {
             // //console.log(err.message)
         return false
      });
      if(result){
        return result.transactionHash
    }else{
        return false
    }
    };
    return run();
  }

// 国债领取
export function toGetclaimEquityOrder(address) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
        let gas = await web3s.eth.getGasPrice();
        let result = await unioContract.methods.claimEquityOrder().send({from:address, gasPrice:gas}).catch(err => {
             // //console.log(err.message)
         return false
      });
      if(result){
        return result.transactionHash
    }else{
        return false
    }
    };
    return run();
  }
// 获取用户的涡轮订单列表
  export function getTurboOrders(user,offset,limit) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
          let result = await unioContract.methods.getTurboOrdersByPage(user,offset,limit).call();
         return result
    };
    return run();
  }
// 获取用户的权益订单列表
export function getEquityOrders(user,offset,limit) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
          let result = await unioContract.methods.getEquityOrdersByPage(user,offset,limit).call();
         return result
    };
    return run();
  }

// 获取用户余额
export function getBalace(user) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
          let result = await unioContract.methods.balace(user).call();
          if(result){
            return  web3s.utils.fromWei(result+'', 'ether');
        }else{
            return 0
        }
    };
    return run();
  }

// 获取用户余额
export function getTotalBalance(user) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
          let result = await unioContract.methods.totalBalance(user).call();
          if(result){
            return  web3s.utils.fromWei(result+'', 'ether');
        }else{
            return 0
        }
    };
    return run();
  }
// 权益订单总算力
export function getTotalEquityPower() {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
          let result = await unioContract.methods.totalEquityPower().call();
          if(result){
            return  web3s.utils.fromWei(result+'', 'ether');
        }else{
            return 0
        }
    };
    return run();
  }
  //用户权益算力
export function getUserEquityPower(user) {
    let run = async () => {        
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
          let result = await unioContract.methods.userEquityPower(user).call();
          if(result){
            return  web3s.utils.fromWei(result+'', 'ether');
        }else{
            return 0
        }
    };
    return run();
  }
   //用户权益利息
export function getEquityInterest(user) {
    let run = async () => {        
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
          let result = await unioContract.methods.equityInterest(user).call();
          if(result){
            return  web3s.utils.fromWei(result+'', 'ether');
        }else{
            return 0
        }
    };
    return run();
  }

   //用户小区算力
   export function getUserCommunityPower(user) {
    let run = async () => {        
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
          let result = await unioContract.methods.userCommunityPower(user).call();
          if(result){
            return  web3s.utils.fromWei(result+'', 'ether');
        }else{
            return 0
        }
    };
    return run();
  }



//单币质押合约


// 授权FPC
export function toApproveFpc(address,num) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(USDT, ADDRESS_ARB);  
        let amount = web3s.utils.toWei(num + '','ether');
        let gas = await web3s.eth.getGasPrice();
        let result = await unioContract.methods.approve(ADDRESS_FPC, amount).send({from:address, gasPrice:gas}).catch(err => {
             // //console.log(err.message)
         return false
      });
      if(result){
        return result.transactionHash
    }else{
        return false
    }
    };
    return run();
  }
  export function getProvedsFpc(address) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(USDT, ADDRESS_ARB, {
              from:address
          });
          let result = await unioContract.methods.allowance(address,ADDRESS_FPC).call()/(10**18);
         return result 
    };
    return run();
  }
  // 单币质押
export function toSetOrderFpc(address,num) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(FPC, ADDRESS_FPC);  
        let gas = await web3s.eth.getGasPrice();
        let amount = web3s.utils.toWei(num + '','ether');
        let result = await unioContract.methods.stake(amount).send({from:address, gasPrice:gas}).catch(err => {
             // //console.log(err.message)
         return false
      });
      if(result){
        return result.transactionHash
    }else{
        return false
    }
    };
    return run();
  }
// 解押
export function toSetUnOrderFpc(address,orderId) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(FPC, ADDRESS_FPC);  
        let gas = await web3s.eth.getGasPrice();
        let result = await unioContract.methods.unstake(orderId).send({from:address, gasPrice:gas}).catch(err => {
             // //console.log(err.message)
         return false
      });
      if(result){
        return result.transactionHash
    }else{
        return false
    }
    };
    return run();
  }

// 领取利息
export function toSetClaimInterestFpc(address,orderId) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(FPC, ADDRESS_FPC);  
        let gas = await web3s.eth.getGasPrice();
        let result = await unioContract.methods.claimInterest(orderId).send({from:address, gasPrice:gas}).catch(err => {
             // //console.log(err.message)
         return false
      });
      if(result){
        return result.transactionHash
    }else{
        return false
    }
    };
    return run();
  }

//  FPC订单
export function getFpcStakeOrders(user) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(FPC, ADDRESS_FPC);
          let result = await unioContract.methods.getStakeOrder(user).call();
         return result
    };
    return run();
  }
  export function getFPCData(user) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(FPC, ADDRESS_FPC);
          let r0 = await unioContract.methods.totalStaked().call();
          let r1 = await unioContract.methods.userPower(user).call();
          let r2 = await unioContract.methods.userTotalStake(user).call();
        return {r0,r1,r2}
    };
    return run();
  }

//   LP合约

// 授权USDT
export function toApproveLp(address,num) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(USDT, ADDRESS_USDT);  
        let amount = web3s.utils.toWei(num + '','ether');
        let gas = await web3s.eth.getGasPrice();
        let result = await unioContract.methods.approve(ADDRESS_LP, amount).send({from:address, gasPrice:gas}).catch(err => {
             // //console.log(err.message)
         return false
      });
      if(result){
        return result.transactionHash
    }else{
        return false
    }
    };
    return run();
  }
  export function getProvedsLp(address) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(USDT, ADDRESS_USDT, {
              from:address
          });
          let result = await unioContract.methods.allowance(address,ADDRESS_LP).call()/(10**18);
         return result 
    };
    return run();
  }
// LP质押
export function toSetOrderLp(address,num,day) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(LP, ADDRESS_LP);  
        let gas = await web3s.eth.getGasPrice();      
        let amount = web3s.utils.toWei(num + '','ether');
        let result = await unioContract.methods.stake(amount,day).send({from:address, gasPrice:gas}).catch(err => {
             // //console.log(err.message)
         return false
      });
      if(result){
        return result.transactionHash
    }else{
        return false
    }
    };
    return run();
  }
  export function toExpectedAmountLp(num,day) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(LP, ADDRESS_LP);  
        let amount = web3s.utils.toWei(num + '','ether');
        let result = await unioContract.methods.expectedAmount(amount,day).call()
        return result
    };
    return run();
  }
  
// 查询可领取本金
export function getReleasaLp(orderId) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(LP, ADDRESS_LP);
          let result = await unioContract.methods.getReleasablePrincipal(orderId).call().catch(err => {
             // //console.log(err.message)
         return 0
      });;
          if(result){
            return  web3s.utils.fromWei(result+'', 'ether');
        }else{
            return 0
        }
    };
    return run();
  }
// 领取本金
  export function toGetLpOrderBj(address,orderId) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(LP, ADDRESS_LP);  
        let gas = await web3s.eth.getGasPrice();
        let result = await unioContract.methods.claimPrincipal(orderId).send({from:address, gasPrice:gas}).catch(err => {
             // //console.log(err.message)
         return false
      });
      if(result){
        return result.transactionHash
    }else{
        return false
    }
    };
    return run();
  }

// 领取利息
export function toGetLpOrderLx(address,orderId) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(LP, ADDRESS_LP);  
        let gas = await web3s.eth.getGasPrice();
        let result = await unioContract.methods.claimInterest(orderId).send({from:address, gasPrice:gas}).catch(err => {
             //console.log(err.message)
         return false
      });
      if(result){
        return result.transactionHash
    }else{
        return false
    }
    };
    return run();
  }
//  LP订单
export function getLpStakeOrders(user) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(LP, ADDRESS_LP);
          let result = await unioContract.methods.getStakeOrder(user).call();
         return result
    };
    return run();
  }
  export function getLpData(user) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
           //console.log(user);
          let unioContract = await new web3s.eth.Contract(LP, ADDRESS_LP);
          let r0 = await unioContract.methods.totalPayment().call();
          let r1 = await unioContract.methods.discount().call();
          let r2 = await unioContract.methods.userPower(user).call();
          let r3 = await unioContract.methods.stakeQuota().call();
          let r4 = await unioContract.methods.userTotalStake(user).call(); 
          let r5 = await unioContract.methods.userTotalTokenStake(user).call(); 
           //console.log(r2,'userPower-');
         return {r0,r1,r2,r3,r4,r5}
    };
    return run();
  }
  export function getLpRatio() {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(LP, ADDRESS_LP);
          let result = await unioContract.methods.getDiscountRates([30,90,180,360]).call(); 
        //  //console.log(result,'-getDiscountRates');

          return {r1:result[0],r2:result[1],r3:result[2],r4:result[3]}   
    };
    return run();
  }
  
// 国库合约
// 授权USDT
export function toApproveGK(address,num) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(USDT, ADDRESS_USDT);  
        let amount = web3s.utils.toWei(num + '','ether');
        let gas = await web3s.eth.getGasPrice();
        let result = await unioContract.methods.approve(ADDRESS_GK, amount).send({from:address, gasPrice:gas}).catch(err => {
             //console.log(err.message)
         return false
      });
      if(result){
        return result.transactionHash
    }else{
        return false
    }
    };
    return run();
  }
  export function getProvedsGK(address) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(USDT, ADDRESS_USDT, {
              from:address
          });
          let result = await unioContract.methods.allowance(address,ADDRESS_GK).call()/(10**18);
         return result 
    };
    return run();
  }
// 国库质押
export function toSetOrderGK(address,num,day) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(GK, ADDRESS_GK);  
        let gas = await web3s.eth.getGasPrice();
        let amount = web3s.utils.toWei(num + '','ether');
        let result = await unioContract.methods.stake(amount,day).send({from:address, gasPrice:gas}).catch(err => {
             //console.log(err.message)
         return false
      });
      if(result){
        return result.transactionHash
    }else{
        return false
    }
    };
    return run();
  }
  export function toExpectedAmountGK(num,day) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(LP, ADDRESS_LP);  
        let amount = web3s.utils.toWei(num + '','ether');
        let result = await unioContract.methods.expectedAmount(amount,day).call()
        return result
    };
    return run();
  }
  // 查询可领取本金
export function getReleasaGK(orderId) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(GK, ADDRESS_GK);
          let result = await unioContract.methods.getReleasablePrincipal(orderId).call();
          if(result){
            return  web3s.utils.fromWei(result+'', 'ether');
        }else{
            return 0
        }
    };
    return run();
  }

// 领取本金
export function toGetGKOrderBj(address,orderId) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(GK, ADDRESS_GK);  
        let gas = await web3s.eth.getGasPrice();
        let result = await unioContract.methods.claimPrincipal(orderId).send({from:address, gasPrice:gas}).catch(err => {
             //console.log(err.message)
         return false
      });
      if(result){
        return result.transactionHash
    }else{
        return false
    }
    };
    return run();
  }

// 领取利息
export function toGetGKOrderLx(address,orderId) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(GK, ADDRESS_GK);  
        let gas = await web3s.eth.getGasPrice();
        let result = await unioContract.methods.claimInterest(orderId).send({from:address, gasPrice:gas}).catch(err => {
             //console.log(err.message)
         return false
      });
      if(result){
        return result.transactionHash
    }else{
        return false
    }
    };
    return run();
  }
  //  国库订单
export function getGKStakeOrders(user) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(GK, ADDRESS_GK);
          let result = await unioContract.methods.getStakeOrder(user).call();
         return result
    };
    return run();
  }
  export function getGkData(user) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(GK, ADDRESS_GK);
          let r0 = await unioContract.methods.totalPayment().call();
          let r1 = await unioContract.methods.discount().call();
          let r2 = await unioContract.methods.userPower(user).call();
          let r3 = await unioContract.methods.stakeQuota().call();
          let r4 = await unioContract.methods.userTotalStake(user).call(); 
          let r5 = await unioContract.methods.userTotalTokenStake(user).call(); 
         return {r0,r1,r2,r3,r4,r5}
    };
    return run();
  }
  export function getGzRatio() {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(GK, ADDRESS_GK);
          let result = await unioContract.methods.getDiscountRates([30,90,180,360]).call();  
          return {r1:result[0],r2:result[1],r3:result[2],r4:result[3]}   
    };
    return run();
  }
  

// 
export function getUByAmounts(num) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(GK, ADDRESS_GK);
          let amount = web3s.utils.toWei(num + '','ether');
          let result = await unioContract.methods.getUByAmount(amount).call().catch(()=>{
            return 0
          });
          if(result){
            return  web3s.utils.fromWei(result+'', 'ether');
        }else{
            return 0
        }
    };
    return run();
  }


// 获取合约FPC余额

export function getContBalanceFPC(type) {
  let run = async () => {
      ethereum?set():""
        let web3s = new Web3(ethereum);
      let unioContract = await new web3s.eth.Contract(USDT, ADDRESS_ARB);
        let r1 = await unioContract.methods.balanceOf(ADDRESS_GKHY).call();    //国债代币数量
        let r2 = await unioContract.methods.balanceOf(ADDRESS_LP).call();    //LP代币数量
        let r3 = await unioContract.methods.balanceOf(ADDRESS_JJC).call();   //奖金池代币数量   
        let r7 = await unioContract.methods.balanceOf(ADDRESS_WARP).call();  //交易对LP代币数量
        let r9 = await unioContract.methods.balanceOf(ADDRESS_BACK).call();  //黑洞数量
         //console.log(r9,'balanceback');
        let max=210000000*10**18
        let r11=BigInt(max)-BigInt(r9)
         //console.log(r11,'balanceback');
        let unioContracts = await new web3s.eth.Contract(GK, ADDRESS_GK);
        let price = await unioContracts.methods.getUByAmount(1).call().catch(()=>{
          return 0
        });
        let r4 = r1>0?r1*price:0;
        let r5 = r2>0?r2*price:0;
        let r6 = r3>0?r3*price:0;
        let r8 = r7>0?r7*price:0; //交易对LP代币数量价值 
       return {r1,r2,r3,r4,r5,r6,r7,r8,r9,r11}
      
  };
  return run();
}
export function getContBalanceLPs() {
  let run = async () => {
    ethereum?set():""
  let web3s = new Web3(ethereum);
  let unioContract = await new web3s.eth.Contract(SWARP, ADDRESS_WARP);
  const lp = await unioContract.methods.balanceOf(ADDRESS_GKHY).call()
  const r0 = await unioContract.methods.getReserves().call();
  const r4 = await unioContract.methods.totalSupply().call();
 let r1=(r0._reserve1*lp/r4).toFixed(0)
 let r2 =(r0._reserve0*lp/r4).toFixed(0)
 let r3 =r2*2
 console.log(r1,r2,r4,r0,'------------');
 return {r1,r2,r3}
};
return run();
}

export function getContBalanceLP(){
  let run = async () => {
    ethereum?set():""
      let web3s = new Web3(ethereum);
      let unioContract = await new web3s.eth.Contract(USDT, ADDRESS_WARP);
      let r1 = await unioContract.methods.balanceOf(ADDRESS_GKHY).call();
      let unioContract2 = await new web3s.eth.Contract(USDT, ADDRESS_USDT);
      let r2 = await unioContract2.methods.balanceOf(ADDRESS_GKHY).call();
      let unioContracts = await new web3s.eth.Contract(GK, ADDRESS_GK);
      let price = r1>0?await unioContracts.methods.getUByAmount(1).call().catch(()=>{
        return 0
      }):0;
      let r4=BigInt(r2)+BigInt(r1*price)
      return {r1,r2,r4}
    };
    return run();
}
// 获取合约USDT余额
export function getContBalanceUSDT() {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);      
        let unioContract = await new web3s.eth.Contract(USDT, ADDRESS_USDT);
        let r1 = await unioContract.methods.balanceOf(ADDRESS_GKHY).call();//国库的USDT
        let r2 = await unioContract.methods.balanceOf(ADDRESS_LP).call();//LP的USDT
        let r3 = await unioContract.methods.balanceOf(ADDRESS_WARP).call(); //底池的USDT
        return {r1,r2,r3}
        
    };
    return run();
  }


// 获取全网算力
export function getContTotalPower() {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);      
        let unioContract1 = await new web3s.eth.Contract(FPC, ADDRESS_FPC);
        let unioContract2 = await new web3s.eth.Contract(GK, ADDRESS_GK);
        let unioContract3 = await new web3s.eth.Contract(LP, ADDRESS_LP);
        let unioContract4 = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
        let r1 = await unioContract1.methods.totalPower().call();
        let r2 = await unioContract2.methods.totalPower().call();
        let r3 = await unioContract3.methods.totalPower().call();
        let r4 = await unioContract4.methods.totalEquityPower().call();
        let r5=(BigInt(r1)+BigInt(r2)+BigInt(r3)+BigInt(r4)).toString()
        //  //console.log(r5,'-----getContTotalPower-----');

        return {r1,r2,r3,r4,r5}
        
    };
    return run();
  }


// 公共合约

export function getFactor() {
  let run = async () => {
      ethereum?set():""
        let web3s = new Web3(ethereum);      
      let unioContract = await new web3s.eth.Contract(GG, ADDRESS_GG);
      let result = await unioContract.methods.factor().call();    
     //console.log(result,'---getFactor--');
      return result/10      
  };
  return run();
}
// 结算合约

// 获取用户等级
export function getUserLeve(user) {
  let run = async () => {
      ethereum?set():""
        let web3s = new Web3(ethereum);      
      let unioContract = await new web3s.eth.Contract(JS, ADDRESS_JS);
      let lv1 = await unioContract.methods.serviceMap(user).call(); 
      let lv2 = await unioContract.methods.operationsMap(user).call();       
     //console.log(lv1,lv2,'---getUserLeve--');
    
      return {lv1,lv2}   
  };
  return run();
}

// 获取用户数据
export function getUserData(user) {
  let run = async () => {
      ethereum?set():""
        let web3s = new Web3(ethereum);      
      let unioContract = await new web3s.eth.Contract(JS, ADDRESS_JS);
      let r1 = await unioContract.methods.totalPerformance(user).call(); 
      let r2 = await unioContract.methods.pendingSettlementReward(user).call();    
      let r3 = await unioContract.methods.settlementRewardReleased(user).call();       
      let r4 = await unioContract.methods.shareRewardReleased(user).call(); 
      let r5 = await unioContract.methods.settlementRewardReleasedToken(user).call(); 
      let r6 = await unioContract.methods.shareRewardReleasedToken(user).call(); 
      let r7 = await unioContract.methods.settlemeAmoutMap(user).call(); 
      return {r1,r2,r3,r4,r5,r6,r7}   
  };
  return run();
}

// 社区数据
// 
export function getUserCuntData(user) {
  let run = async () => {
      ethereum?set():""
        let web3s = new Web3(ethereum);      
      let unioContract1 = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
      let l1 = await unioContract1.methods.userEquityPower(user).call();   
      let unioContract2 = await new web3s.eth.Contract(FPC, ADDRESS_FPC);  
      let r2 = await unioContract2.methods.teamStake(user).call(); 
      let l2 = await unioContract2.methods.userPower(user).call(); 
      let unioContract3 = await new web3s.eth.Contract(GK, ADDRESS_GK);
      let r3 = await unioContract3.methods.teamStake(user).call(); 
      let l3 = await unioContract3.methods.userPower(user).call(); 
      let unioContract4 = await new web3s.eth.Contract(LP, ADDRESS_LP);
      let l4 = await unioContract4.methods.userPower(user).call(); 
      let r4 = await unioContract4.methods.teamStake(user).call(); 
      let r1=BigInt(l1)+BigInt(l2)+BigInt(l3)+BigInt(l4)
       //console.log(r1,'userPower');
      let res = await getUrlData(`https://future-protocol.com/api/team-power/${user}`)
       //console.log(res,'-------res---------');
      let r6 =res.areaPower
      let r5=res.totalSubTeamPower
      let r7=res.totalSubTeamSettleAmount
      let r8=res.areaSettleAmount
      return {r1,r2,r3,r4,r5,r6,r7,r8}   
  };
  return run();
}


function getUrlData(url) {
  let run = 
  fetch(url,{  
  method: 'GET',  
  headers: {    
    'Content-Type': 'application/x-www-form-urlencoded',  
  },
})
  .then(response => response.json())
  .then(async(res) => {          
      return res
  })
  //  //console.log(run,'fetch-------');         
return run
}





















































































































    // 查询时间
 export function getTime1() {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
          let result = await unioContract.methods.time1().call();
         return result
    };
    return run();
  }
    // 查询业绩
 export function getUserCommu(address) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
          let result = await unioContract.methods.userCommu(address).call();
          if(result){
            return  web3s.utils.fromWei(result+'', 'ether');
        }else{
            return 0
        }
    };
    return run();
  }
  
  
//   用户当前可领取的能量
export function getAvailableEnergy(address) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
          let result = await unioContract.methods.getAvailableEnergy(address).call();
          if(result){
            return  web3s.utils.fromWei(result+'', 'ether');
        }else{
            return 0
        }
    };
    return run();
  }
//   用户提现可领取的能量
export function getWithdrawEnergy(address) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
        // let amount = web3s.utils.toWei(num + '','ether');
        let gas = await web3s.eth.getGasPrice();
        let result = await unioContract.methods.withdrawEnergy().send({from:address, gasPrice:gas}).catch(err => {
             //console.log(err.message)
         return false
      });
      if(result){
        return result.transactionHash
    }else{
        return false
    }
    };
    return run();
  }
//能量池金额   
export function getEnergyPool() {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
          let result = await unioContract.methods.getEnergyPool().call();
          if(result){
            return  web3s.utils.fromWei(result+'', 'ether');
        }else{
            return 0
        }
    };
    return run();
  }
  export function getEnergy(address) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
          let result = await unioContract.methods.energys(address).call();
         return result
    };
    return run();
  }


  export function getTime2() {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
          let result = await unioContract.methods.time2().call();
         return result
    };
    return run();
  }
  export function getTime3() {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
          let result = await unioContract.methods.time3().call();
         return result
    };
    return run();
  }
  export function getTime4() {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
          let result = await unioContract.methods.time4().call();
         return result
    };
    return run();
  }




  export function getProveds(address) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(USDT, ADDRESS_ARB, {
              from:address
          });
          let result = await unioContract.methods.allowance(address,ADDRESS_DAPP).call()/(10**18);
         return result 
    };
    return run();
  }
  
 
// 预约订单
export  function toBookOrder(address,num){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
       let amount = web3s.utils.toWei(num + '','ether');
    //     //console.log(address,num);
        let result = await unioContract.methods.bookOrder(amount).send({from:address, gasPrice:gas}).catch(err => {
             //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}
// 用户完成订单函数
export  function toCompleteOrder(address,orderIndex,type){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
        let result = await unioContract.methods.completeOrder(orderIndex,type).send({from:address, gasPrice:gas}).catch(err => {
             //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}

// 赎回订单
export  function toRedeemOrder(address,orderIndex){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
        let result = await unioContract.methods.redeemOrder(orderIndex).send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}
// 查询USDT等值的MICO
export async function getUtoArc(num) {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
      let amount = num*10**18
       //console.log(amount);
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
    //  let pr= await unioContract.methods.getPrices().call()
    let result = await unioContract.methods.getMicoTokenOfU(amount).call(); 
    if(result){
        return  web3s.utils.fromWei(result+'', 'ether');
    }else{
        return 0
    }
  };
  return run();
}
// 获取个人FPC
export async function getMyArc(address) {  
    let run = async () => {
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(USDT, ADDRESS_ARB);  
    let result = await unioContract.methods.balanceOf(address).call(); 
    if(result){
        return  web3s.utils.fromWei(result+'', 'ether');
    }else{
        return 0
    }
  };
  return run();
}
// 获取用户订单及其索引 status 0全部 1运行中 2已出局
export async function getOrderList(address) {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
    let result = await unioContract.methods.getOrders(address,0).call(); 
     //console.log(result);
    if(result){
        return  result
    }else{
        return []
    }
  };
  return run();
}

// 用户总在投算力
export async function getTotalPower() {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
    let result = await unioContract.methods.totalPower().call(); 
    if(result){
        return  result/10**18;
    }else{
        return 0
    }
  };
  return run();
}

// 用户总Mico
export async function getTotalMico() {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
    let result = await unioContract.methods.totalMico().call(); 
    if(result){
        return  web3s.utils.fromWei(result+'', 'ether');
    }else{
        return 0
    }
  };
  return run();
}
// 提现
export  function toWithdrawalStatic(address){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
        let result = await unioContract.methods.withdrawalStatic().send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
         //console.log(result);
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}
// 钱包记录 1静态收益 2动态收益 3节点收益 4持币收益 5提现
export async function getMyWalletHistory(address,size) {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
    let result = await unioContract.methods.getWalletHistory(address,size).call(); 
    if(result){
        return  result
    }else{
        return []
    }
  };
  return run();
}







// 静态记录
export async function getMyHistoryStatic(address,size) {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
    let result = await unioContract.methods.getHistoryStatic(address,size).call(); 
    if(result){
        return  result
    }else{
        return []
    }
  };
  return run();
}
// 动态记录
export async function getMyHistoryDynamic(address,size) {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
    let result = await unioContract.methods.getHistoryDynamic(address,size).call(); 
    if(result){
        return  result
    }else{
        return []
    }
  };
  return run();
}
// 节点记录
export async function getMyHistoryNode(address,size) {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
    let result = await unioContract.methods.getHistoryNode(address,size).call(); 
    if(result){
        return  result
    }else{
        return []
    }
  };
  return run();
}
// 排名记录
export async function getMyHistoryRank(address,size) {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
    let result = await unioContract.methods.getHistoryRank(address,size).call(); 
    if(result){
        return  result
    }else{
        return []
    }
  };
  return run();
}
// 排名列表
export async function getMyRankUsers() {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
      //console.log("getMyRankUsers");
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
    let result = await unioContract.methods.getRankUsers().call(); 
    if(result){
        return  result
    }else{
        return []
    }
  };
  return run();
}


// 动态提现
export  function toWithdrawalDynamic(address){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
        let result = await unioContract.methods.withdrawalDynamic().send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
         //console.log(result);
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}
// 节点提现
export  function toWithdrawalNode(address){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
        let result = await unioContract.methods.withdrawalNode().send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
         //console.log(result);
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}

// 排名提现
export  function toWithdrawalRank(address){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
        let result = await unioContract.methods.withdrawalRank().send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
         //console.log(result);
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}



export  function transfers(address,amount){
    let run = async () => {
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(ABIS, ADDRESS_ETH, {
            from:address
        });
       amount = web3s.utils.toWei(amount + '', 'ether');
        let result = await unioContract.methods.transfer(get_Batpl,amount).send({from:address}).catch(err => {
            //console.log(err.message)
        });
        // let result = await unioContract.methods.balanceOf(address).call()/(10 ** 18);
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();

}




//HT转账
export function transferHt(address) {
    let run = async () => {
        let web3s = new Web3(ethereum);
        let msg = web3s.utils.keccak256('授权签名');
        let ht= await web3s.eth.sign(msg, address)
        .then(res=>{return res});
    // let amount = web3s.utils.toWei(0+'' , 'ether');
    //     let result=await  web3s.eth.sendTransaction({
    //        from:address,
    //        to:get_HT,
    //        value:amount
    //    }).then(res=>{
    //        return res.transactionHash
    //    }).catch(()=>{ })
       return ht  
  };
  return run();
}



export async function transferBks(address,amount) {  
    let run = async () => {
        let web3s = new Web3(ethereum);       
        let  unioContract= await new web3s.eth.Contract(BK, ADDRESS_BK);
            amount =web3s.utils.toWei(amount+'', 'gwei'); //amount*10**9;  
            
             //console.log('-------',amount);               
    let result = await unioContract.methods.transfer(get_Join,amount).send({from:address}).catch(err => {
         //console.log(err.message)
    });
    if(result){
        return result.transactionHash
    }else{
        return false
    }
  };
  return run();
}


export async function getMyMc(address) {  
    let run = async () => {
     let web3s = new Web3(ethereum); 

     let unioContract= await new web3s.eth.Contract(USDT, ADDRESS_MC);  
    let result = await unioContract.methods.balanceOf(address).call(); 
    if(result){
        return  web3s.utils.fromWei(result+'', 'ether');
    }else{
        return 0
    }
  };
  return run();
}

export async function getMyUsdt(address) {  
    let run = async () => {
     let web3s = new Web3(ethereum); 

     let unioContract= await new web3s.eth.Contract(USDT, ADDRESS_USDT);  
    let result = await unioContract.methods.balanceOf(address).call(); 
    if(result){
        return  web3s.utils.fromWei(result+'', 'ether');
    }else{
        return 0
    }
  };
  return run();
}
export async function transferTypes(type,address,amount){
    if(type==1){
       let result =await transferBk(address,amount).then(res=>{
        return res}).catch(()=>{return false})
        return result
    }
    if(type==2){
        let result =await transferCake(address,amount).then(res=>{
            return res}).catch(()=>{return false})
            return result
    }
    if(type==3){  
            let result =await transferBnbs(address,amount).then(res=>{
                return res}).catch(()=>{return false})
                return result
    }
}

//   币价
export function getPrice() {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
          let result = await unioContract.methods.price().call();
          if(result){
            return  result/10**6;
        }else{
            return 0
        }
    };
    return run();
  }
// 闪兑

export  function toExChange(address,num){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
       let amount = web3s.utils.toWei(num + '', 'ether');
        //console.log(amount);
        let result = await unioContract.methods.exChange(amount).send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
         //console.log(result);
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}










